import javax.swing.text.JTextComponent;
import org.w3c.dom.Node;


/**
 * This is the new ButtonPanel. It no longer contains two ClausePanels. The UI
 * looks the same, but it was built differently. I used the NetBeans IDE GUI
 * builder. The associated ButtonPanel.form is included.
 */
public class ButtonPanel extends javax.swing.JPanel {

    private XMLTreeNode firstNodeSelection;     // Holds the Node used in the firstNodeSelection box
    private XMLTreeNode lastNodeSelection;      // Holds the Node used in the lastNodeSelection box

    /**
     * Toggle the enabled property for the buttons.
     *
     * @param enabled the value to assign to the enabled attribute of the
     * buttons
     */
    public final void setButtonsEnabled(boolean enabled) {
        //Button setting to disabled (some of these are initially disabled because of no functionality
        cmdOkay.setEnabled(enabled);
        cmdSplit.setEnabled(enabled);
    }

    /**
     * Creates new form ButtonPanel
     */
    public ButtonPanel() {
        initComponents();
        Clause temp = null;
        setButtonsEnabled(false);
    }
    
    /**
     * Returns the Node represented in the top box.
     *
     * @return the Node in the top box
     */
    public XMLTreeNode getFirstNodeSelection() {
        return firstNodeSelection;
    }

    /**
     * Returns the Node represented in the bottom box.
     *
     * @return the Node in the bottom box
     */
    public XMLTreeNode getLastNodeSelection() {
        return lastNodeSelection;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmdOkay = new javax.swing.JButton();
        cmdCancel = new javax.swing.JButton();
        cmdSplit = new javax.swing.JButton();
        cmdRemove = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setFocusable(false);

        cmdOkay.setText("Okay");
        cmdOkay.setFocusable(false);
        cmdOkay.setNextFocusableComponent(cmdCancel);
        cmdOkay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdOkayActionPerformed(evt);
            }
        });

        cmdCancel.setText("Cancel");
        cmdCancel.setFocusable(false);
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        cmdSplit.setText("Split");
        cmdSplit.setFocusable(false);
        cmdSplit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSplitActionPerformed(evt);
            }
        });

        cmdRemove.setText("Remove");
        cmdRemove.setFocusable(false);
        cmdRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRemoveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmdOkay, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdSplit, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cmdOkay)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdSplit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdRemove)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmdCancel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
        cmdOkay.setEnabled(false);
        workingClause.chooseFocus(false);
        workingClause.setTextArea(revertText);
        Clause.clickCount = 0;  //CD
        DiscourseAnalysisApplet.nodePanel.hideButtonPanel();
        workingClause.getJTextPane().getCaret().setVisible(false); //CD
    }//GEN-LAST:event_cmdCancelActionPerformed

    
    //Action performed on "Okay" button clicking.
    private void cmdOkayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOkayActionPerformed
        //This will commit edits made by user in the text area to the actual node.
        //Note that this will change the node in the program but will not be saved permenantly
        //until the saveXML is called.
        editedText = "";
        editedText = workingClause.getTextArea();
        XMLTreeModel.setNodeData(workingNode, editedText);
        //This part will reset the node area to a "Dormant" state and reset the buttons.
        DiscourseAnalysisApplet.nodePanel.hideButtonPanel();
        cmdOkay.setEnabled(false);
        workingClause.chooseFocus(false);
        Clause.clickCount = 0;  //CD
        workingClause.getJTextPane().getCaret().setVisible(false);  //CD
    }//GEN-LAST:event_cmdOkayActionPerformed

    //This may need to be re-worked at some point, however, this allows the button panel actions
    //to get the informatin on the clause and node.
    //In Clause, when a area is clicked, it will send the information to this function
    public void associateClauseAndNode(Clause c, XMLTreeNode n) {
        workingClause = c;
        workingNode = n;
        revertText = workingClause.getTextArea();
    }
    private String revertText;
    private String editedText;
    private String selectedText;
    private Clause workingClause;
    private XMLTreeNode workingNode;
    
    public void editEnable(){
      workingClause.chooseFocus(true);
      cmdOkay.setEnabled(true);
      cmdSplit.setEnabled(true);
    }
    //Remove button, not currently working.
    private void cmdRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRemoveActionPerformed
       
    }//GEN-LAST:event_cmdRemoveActionPerformed

    //AiDS
    //Action Performed by split button
    private void cmdSplitActionPerformed(java.awt.event.ActionEvent evt){
    	System.out.println("Trying to split.");
        editedText = "";    //CD - Allows live changes to be made without saving.
        editedText = workingClause.getTextArea();   //CD - Allows live changes to be made without saving.
        XMLTreeModel.setNodeData(workingNode, editedText);
        Clause.clickCount = 0;  //CD
        workingClause.chooseFocus(true);
        selectedText = workingClause.toString();
        DiscourseAnalysisApplet.getXMLTreeModel().split(workingNode, selectedText);
        //DiscourseAnalysisApplet.nodePanel.hideButtonPanel();
        cmdOkay.setEnabled(false);
        cmdSplit.setEnabled(false);
        workingClause.chooseFocus(false);
        workingClause.getJTextPane().getCaret().setVisible(false);  //CD
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdOkay;
    private javax.swing.JButton cmdRemove;
    private javax.swing.JButton cmdSplit;
    // End of variables declaration//GEN-END:variables
}
